package com.hkws.hkws.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hkws.hkws.API.DoorGateApi;
import com.hkws.hkws.mapper.DevlistMapper;
import com.hkws.hkws.mapper.DevstateMapper;
import com.hkws.hkws.pojo.DevStatequest;
import com.hkws.hkws.pojo.Devlist;
import com.hkws.hkws.pojo.Devstate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 查询设备在线列表
 */

@Component
public class QueryDevStates {

    @Autowired
        private DevstateMapper devstateMapper;
    @Autowired
    private DevlistMapper devlistMapper;
    private Object o;
    // @Scheduled(initialDelay = 15, fixedRate = 1000)
    public void execute() {
        DoorGateApi doorGateApi = new DoorGateApi();
         Devstate devstate = new Devstate();
         DevStatequest devStatequest = new DevStatequest();
         List<Devlist> devlists = devlistMapper.selectList(null);
         Iterator<Devlist> iterator1 = devlists.iterator();
         ArrayList<String> arrayList=new ArrayList<String>();
         while (iterator1.hasNext())
        {
            Devlist devlist = iterator1.next();
            arrayList.add(devlist.getAcsDevIndexCode());
        }
         devStatequest.setIndexCodes(arrayList);
        /**
         * 查询读卡器列表
         */
        String s =doorGateApi.Devget(devStatequest);
        JSONObject jsonObject = JSONObject.parseObject(s);
        System.out.println("jsonObject"+jsonObject);
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");
        System.out.println("list"+list);
        for (int i = 0; i < list.size(); i++) {
            JSONObject json1 = (JSONObject) list.get(i);
            // System.out.println("json"+"第几"+i+json1);
            Field[] fields = devstate.getClass().getDeclaredFields();
            for (Field field : fields) {
                //System.out.println(field.getName());
                Iterator<String> iterator = json1.keySet().iterator();
                while (iterator.hasNext()) {
                    String name = iterator.next();
                    if (field.getName().equals(name)) {
                        Object o = json1.get(name);
                        System.out.println("O========="+o);
                        if (field.getType().toString().equals("class java.lang.String")) {
                            String o1;
                            if(o==null)
                            {
                                o1="";

                            }else
                            {
                                o1 = o.toString();
                            }
                            try {
                                field.setAccessible(true);
                                field.set(devstate, o1);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }else if(field.getType().toString().equals("class java.lang.Integer"))
                        {
                            Integer o1=null;
                            o1=Integer.valueOf(o.toString());
                            try {
                                field.setAccessible(true);
                                field.set(devstate, o1);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        else {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                            Date ts = null;
                            try {
                                ts = sdf.parse((String) o);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            try {
                                field.setAccessible(true);
                                field.set(devstate, ts);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            System.out.println("doorstate"+devstate);
            devstateMapper.insert(devstate);
        }
    }

}
